package com.lxh.miniapp.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.lxh.miniapp.utils.JsonUtils;
import com.lxh.miniapp.utils.MiniAppUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * created by lanxinghua@2dfire.com on 2020/2/23
 * 微信小程序用户接口
 */
@RestController
@RequestMapping("/wx/user/{appid}")
public class WxMiniAppUserController {
    private WxMaService wxMaService;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 登陆接口
     * http://chenxingxing.51vip.biz/wx/user/wx2d88824e64aacf4d/login?code=021cwfBL0m7xXb2eQxBL0lLhBL0cwfBo
     *
     * {
     * session_key: "7TdxzPbxChCVPqqCpHWIsw==",
     * openid: "o0aPU5JJUSr1DoNtkJXqSFa1CqoU"
     * }
     */
    @GetMapping("/login")
    public String login(@PathVariable String appid,
                        String code) {
        if (StringUtils.isBlank(code)) {
            return "empty jscode";
        }
        wxMaService = MiniAppUtils.switchover(appid);
        try {
            WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(code);
            this.logger.info("sessionKey："+session.getSessionKey());
            this.logger.info("openId："+session.getOpenid());
            return JsonUtils.toJson(session);
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            return e.getMessage();
        }
    }

    /**
     * 获取用户信息接口
     */
    @GetMapping("/info")
    public String info(@PathVariable String appid,
                       String sessionKey,
                       String signature,
                       String rawData,
                       String encryptedData,
                       String iv) {
        wxMaService = MiniAppUtils.switchover(appid);
        // 用户信息校验
        if (!wxMaService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
            return "user check failed";
        }
        // 解密用户信息
        WxMaUserInfo userInfo = wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
        return JsonUtils.toJson(userInfo);
    }

    /**
     * 获取用户绑定手机号信息
     */
    @GetMapping("/phone")
    public String phone(@PathVariable String appid,
                        String sessionKey,
                        String encryptedData,
                        String iv) {
        wxMaService = MiniAppUtils.switchover(appid);
        // 解密
        WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);
        return JsonUtils.toJson(phoneNoInfo);
    }


}
